import request from "@/utils/request";

let dicts = new Map();

export function getDictValue(type, key) {
    const typeMap = dicts.get(type);
    if (!typeMap) {
        return '';
    }
    const item = typeMap.get(String(key));
    if (!item) {
        return '';
    }
    return item.label;
}

export function initDict(keys) {
    keys.forEach(key => {
        if (dicts.has(key)) {
            return;
        }
         getDict(key).then(res => {
             if (res.data !== null) {
                 let dict = new Map();
                 res.data.forEach(item => {
                     dict.set(item.value, item);
                 });
                 dicts.set(key, dict);
             }
         });
     });
}

export function getDict(type) {
    return request({
        url: '/app/work/getDicts?type=' + type,
        method: 'get',
    })
}
